// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Data.Department");

// 部门管理 - 部门选择下拉框组件
yepjs.define("deptSelect", {

    NS: "YBA.Logic.Department",

    field: function(options) {

        var Biz = {
            // 初始化事件
            init: function() {

                Biz.vars.selectedVal = Biz.el.jvalue.val();

                Biz.fn.getData(function(){
                    for (var i in Biz.event) {
                        Biz.event[i]();
                    }
                },options.allOn);
            },
            //  公共变量
            vars: {
                selectedVal: null
            },
            // 页面元素
            el: {
            	jbody:$("body"),
                jtitleCon: options.con.find(".j-treeSelectTitle"),
                jtxt: options.con.find(".j-treeSelectTxt"),
                jlistCon: options.con.find(".j-treeSelectList"),
                jvalue: options.con.find(".j-treeSelectValue")
            },
            // 功能函数集合
            fn: {
                // 读取机构列表数据
                getData: function(callback,allOn){
                    YBA.Data.Department.getDeptList({
                    	startPos: 1,
                        pageSize: 0,
                        parentId: 0
                    }, function(json){
                        if(json.code == 200){
                            Biz.fn.createList(json.objList,allOn);
                            callback && callback();
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: "读取机构数据失败"});
                        }
                    });
                },
                // 生成列表
                createList: function(data){

                    var html = [], uper = -1;

                    var createList = function(dataItem){
                        uper++;
                        var tmpHtml = [];
                        tmpHtml.push('<ul>');
                        for(var i=0,len=dataItem.length;i<len;i++){
                        	if(i == 0 && uper == 0){
                        		Biz.el.jvalue.val(dataItem[i].id);
                                Biz.el.jtxt.val(dataItem[i].name);
                        	}
                            tmpHtml.push('    <li data-value="' + dataItem[i].id + '" data-text="' + dataItem[i].name + '">');
                            tmpHtml.push('        <div style="padding-left:' + uper*20 + 'px" class="ui-treeSelectItem j-item '+ (dataItem[i].id == Biz.vars.selectedVal ? "select": "") +'">');
                            if(dataItem[i].hasChildren){
                                tmpHtml.push('            <span class="tree-ico"><i class="ui-tree-ico ui-ico-open"></i></span>');
                            }
                            tmpHtml.push('            <span>' + dataItem[i].name + '</span>');
                            tmpHtml.push('        </div>');
                            if(dataItem[i].hasChildren){
                                tmpHtml.push(createList(dataItem[i].children));
                            }
                            tmpHtml.push('    </li>');
                        }
                        tmpHtml.push('</ul>');
                        uper--;
                        return tmpHtml.join("");
                    };
                    html.push(createList(data));
                    Biz.el.jlistCon.html(html.join(""));
                }
            },
            // 初始化时执行的所有事件
            event: {
                // 显示/隐藏下拉框列表项
                toggleListCon: function () {
                    Biz.el.jtitleCon.bind("click", function(e){
                        Biz.el.jlistCon.toggle();
                        e.stopPropagation();
                    });
                    Biz.el.jbody.bind("click",function(e){
                    	Biz.el.jlistCon.hide();
                    	e.stopPropagation();
                    });
                },
                // 选择列表项
                chooseItem: function(){
                    Biz.el.jlistCon.delegate(".j-item", "click", function(e){
                        var t = $(this), li = t.closest("li"), id = li.attr("data-value"), txt = li.attr("data-text");
                        Biz.el.jvalue.val(id);
                        Biz.el.jtxt.val(txt);
                        Biz.el.jlistCon.hide();

                        Biz.vars.selectedVal = id;

                        Biz.el.jlistCon.find(".j-item").removeClass("select");
                        t.addClass("select");
                    });
                },
                // 分支展开折叠
                toggleTree: function(){
                    Biz.el.jlistCon.delegate(".ui-tree-ico", "click", function(e){
                        var t = $(this), children = t.closest("li").find(">ul");
                        if(t.hasClass("ui-ico-open")){
                            t.removeClass("ui-ico-open").addClass('ui-ico-close');
                            children.hide();
                        }else{
                            t.removeClass("ui-ico-close").addClass('ui-ico-open');
                            children.show();
                        }
                        e.stopPropagation();
                    });
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
